View Javadoc

1   package uba.db.ar;
2   
3   import java.util.*;
4   
5   public class TupleCollectionBuilder {
6   
7   	public TupleCollectionBuilder() {
8   		super();
9   	}
10  
11  	public TupleCollection newFromProvider(TupleProvider provider) {
12  		TupleCollection result = new TupleCollection(provider.tupleDefinition());
13  		provider.reset();
14  		while (provider.hasNext()) {
15  			result.addTupla(provider.next());
16  		}
17  		return result;
18  	}
19  
20  	public TupleCollection newInstance(TuplaDef tupleDefinition, int size) {
21  		TupleCollection result = new TupleCollection(tupleDefinition);
22  		Tupla tupla;
23  		int count = 0;
24  		while (count < size) {
25  			tupla = new Tupla(tupleDefinition);
26  			fillTuplaWithValues(tupla);
27  			result.addTupla(tupla);
28  			count++;
29  		}
30  		return result;
31  	}
32  
33  	private void fillTuplaWithValues(Tupla tupla) {
34  		TuplaDef def = tupla.tuplaDefinition();
35  		Iterator i = def.atributos().iterator();
36  		AttributeDef atributo;
37  		int c = 1;
38  		while (i.hasNext()) {
39  			atributo = (AttributeDef) i.next();
40  			tupla.set(c, valueFor(atributo));
41  			c++;
42  		}
43  	}
44  
45  	private Object valueFor(AttributeDef attributeDefinition) {
46  		Object result;
47  		if (attributeDefinition.isInteger()) {
48  			result = randomInteger();
49  		} else {
50  			result = randomString();
51  		}
52  		return result;
53  	}
54  
55  	private Integer randomInteger() {
56  		Double d;
57  		d = new Double(Math.random() * 1000);
58  		return (new Integer(d.intValue()));
59  	}
60  
61  	private String randomString() {
62  		Vector valores = new Vector();
63  		valores.add("HOMBRE");
64  		valores.add("MUJER");
65  		valores.add("PERRO");
66  		valores.add("ROJO");
67  		valores.add("AZUL");
68  		valores.add("NADA");
69  		valores.add("FOX");
70  		valores.add("CHARLIE");
71  		valores.add("HOTEL");
72  		valores.add("GUN");
73  		int indice;
74  		Double d;
75  		d = new Double(Math.random() * 10);
76  		System.out.println(d.toString());
77  		indice = d.intValue();
78  		return (String) valores.elementAt(indice);
79  	}
80  }